<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\Refund;
use App\Admin\Actions\Post\Shipments;
use App\Models\Orders;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Actions\Post\Replicate;

class SysOrdersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '订单';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Orders());
        $grid->fixColumns(4, -4);
        //$grid->column('id', __('Id'));
        $grid->column('order_sn', __('订单编号'));
        $grid->column('goods_name', __('商品名称'))->label('primary')->sortable();
        $grid->column('goods_num', __('下单数量'));

        $grid->column('transaction_sn', __('微信支付订单'))->help('微信平台订单号');
        $grid->column('user_name', __('收货人'))->label('danger')->sortable();
        $grid->column('user_mobile', __('收货手机号'))->display(function () {
            return '<i class="fa fa-phone"></i> ' . +$this->user_mobile;
        });
        $grid->column('shipping_address', __('收货地址'))->label('danger')->sortable();
        $grid->column('courier_sn', __('快递单号'))->display(function () {
            if (!empty($this->courier_sn)) {
                return $this->courier_sn;
            }
            return '';
        })->sortable();
        //$grid->column('status_msg', __('状态说明'));
        $grid->column('total_price', __('总价'))->display(function () {
            return '<i class="fa fa-cny"></i> ' . +$this->total_price;
        });
        $grid->column('prices', __('支付金额'))->display(function () {
            return '<i class="fa fa-cny"></i> ' . +$this->prices;
        });
        //$grid->column('bank_type', __('支付方式'));
        $grid->column('pay_at', __('支付时间'))->sortable();
        $grid->column('refund_at', __('退款时间'));
        $grid->column('status', __('订单状态'))->using([
            1 => '待付款',
            2 => '已付款/待发货',
            3 => '已发货',
            4 => '已完成',
            5 => '已取消',
            6 => '已退款',
        ], '未知')->dot([
            1 => 'danger',
            2 => 'primary',
            3 => 'success',
            4 => 'success',
            5 => 'danger',
            6 => 'success',
        ], 'warning')->sortable();
        $grid->column('created_at', __('下单时间'))->sortable();
        $grid->column('updated_at', __('修改时间'))->sortable();

        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
            $row = $actions->row;

            if ($row->status == 2) { //代发货
                $actions->add(new Shipments);
            }
            if (in_array($row->status, [2, 3, 4])) { //退款
                $actions->add(new Refund);
            }

        });

        $grid->disableCreateButton();

        $grid->expandFilter();


        //筛选
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->like('order_sn', __('订单编号'));
                $filter->like('goods_name', __('商品名称'));
                $filter->like('transaction_sn', __('微信订单号'));
                $filter->like('courier_sn', __('快递单号'));
                $filter->equal('status', __('订单状态'))->radio([
                    '' => '全部',
                    1 => '待付款',
                    2 => '已付款/待发货',
                    3 => '已发货',
                    4 => '已完成',
                    5 => '已取消',
                    6 => '已退款',
                ]);


            });

            $filter->column(1 / 2, function ($filter) {
                $filter->like('user_name', __('收货人'));

                $filter->like('user_mobile', __('收货人手机号'));

                $filter->between('created_at', __('下单时间'))->datetime();

                $filter->between('pay_at', __('支付时间'))->datetime();

                $filter->between('refund_at', __('退款时间'))->datetime();


            });

        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Orders::findOrFail($id));

        $show->column(1/2 , function ($show){
            $show->field('id', __('Id'));
            $show->field('order_sn', __('Order sn'));
            $show->field('transaction_sn', __('Transaction sn'));
            $show->field('courier_sn', __('Courier sn'));
            $show->field('user_id', __('User id'));
            $show->field('goods_id', __('Goods id'));
            $show->field('goods_name', __('Goods name'));
            $show->field('goods_num', __('Goods num'));
        });

        $show->column(1/2 , function ($show){
            $show->field('id', __('Id'));
            $show->field('order_sn', __('Order sn'));
            $show->field('transaction_sn', __('Transaction sn'));
            $show->field('courier_sn', __('Courier sn'));
            $show->field('user_id', __('User id'));
            $show->field('goods_id', __('Goods id'));
            $show->field('goods_name', __('Goods name'));
            $show->field('goods_num', __('Goods num'));
        });


        $show->field('status', __('Status'));
        $show->field('status_msg', __('Status msg'));
        $show->field('prices', __('Prices'));
        $show->field('total_price', __('Total price'));
        $show->field('user_name', __('User name'));
        $show->field('user_mobile', __('User mobile'));
        $show->field('shipping_address', __('Shipping address'));
        $show->field('bank_type', __('Bank type'));
        $show->field('courier_code', __('Courier code'));
        $show->field('prepay_id', __('Prepay id'));
        $show->field('pay_at', __('Pay at'));
        $show->field('refund_at', __('退款时间'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Orders());

        $form->column(1/2, function ($form) {
            $form->display('id', '编号')->help('不支持修改');

            $form->display('order_sn', __('订单编号'))->help('不支持修改');

            $form->display('transaction_sn', __('微信订单编号'))->help('不支持修改');

            $form->display('goods_name', __('商品名称'))->help('不支持修改');

            $form->display('goods_num', __('下单数量'))->help('不支持修改');

            $form->display('status', __('订单状态'))->with(function ($value) {
                switch ($value) {
                    case 1:
                        return '待付款';
                        break;
                    case 2:
                        return '已付款/待发货';
                        break;
                    case 3:
                        return '已发货';
                        break;
                    case 4:
                        return '已完成';
                        break;
                    case 5:
                        return '已取消';
                        break;
                    case 6:
                        return '已退款';
                        break;
                    default:
                }
            })->help('不支持修改');

            $form->display('pay_at', __('支付时间'))->default(date('Y-m-d H:i:s'))->help('不支持修改');

        });


        $form->column(1/2, function ($form) {
            $form->text('status_msg', __('状态说明'))->help('订单状态说明');

            $form->display('prices', __('实付款金额'))->help('不支持修改');

            $form->display('total_price', __('总价'))->help('不支持修改');

            $form->display('bank_type', __('支付方式'))->help('不支持修改');

            $form->display('refund_at', __('支付时间'))->default(date('Y-m-d H:i:s'))->help('不支持修改');

            $form->text('user_name', __('收货人姓名'));

            $form->text('user_mobile', __('收货人手机号'));

            $form->text('shipping_address', __('发货地址'));

            $form->text('courier_sn', __('快递编号'))->help('用于用户手动查询快递信息');

        });


        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

        });


        return $form;
    }
}
